<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Created by PhpStorm.
 *
 * @Date: 2017-06-08
 * @Time: 16:57
 * @Author: cdkay
 * @Email: network@iyuanma.net
 *
 * @File： User.php
 */
class User extends MY_Controller {
    public function __construct()
    {
        parent::__construct();

        $this->load->model('system/User_model');
        $this->load->model('system/Role_model');
    }

    /**
     * index
     * 管理后台系统用户列表
     */
    public function index()
    {
        $data['user'] = $this->User_model->get_all_user();

        $this->load->view('system/user/index', $data);
    }

    /**
     * add
     * 添加管理后台系统用户
     */
    public function add()
    {
        // 获取系统角色数据
        $data['role'] = $this->Role_model->get_all_role();

        $this->load->view('system/user/add', $data);
    }

    /**
     * repeat
     * 检测管理后台系统用户名是否重复
     */
    public function repeat()
    {
        if (!IS_POST) exit();

        $result = $this->User_model->repeat_user();

        exit(json_encode($result));
    }

    /**
     * edit
     * 编辑管理后台系统用户
     *
     * @param int $user_id 用户id
     */
    public function edit($user_id)
    {
        // 获取系统角色数据
        $data['role'] = $this->Role_model->get_all_role();

        $data['user'] = $this->User_model->get_single_user($user_id);

        $this->load->view('system/user/edit', $data);
    }

    /**
     * save
     * 保存管理后台用户数据 添加|编辑
     */
    public function save()
    {
        if (!IS_POST) exit();

        $result = $this->User_model->user_save();

        exit(json_encode($result));
    }

    /**
     * del
     * 删除管理后台系统用户
     */
    public function del()
    {
        $result = $this->User_model->user_delete();

        exit(json_encode($result));
    }
}